package com.ruoyi.wx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.wx.entity.PtExpClass;
import com.ruoyi.wx.entity.PtScore;
import com.ruoyi.wx.entity.PtVideo;
import com.ruoyi.wx.entity.vo.PtScoreVO;
import com.ruoyi.wx.mapper.PtExpClassMapper;
import com.ruoyi.wx.mapper.PtScoreMapper;
import com.ruoyi.wx.mapper.PtVideoMapper;
import com.ruoyi.wx.service.IPtScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 实验成绩表 服务实现类
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@Service
public class PtScoreServiceImpl extends ServiceImpl<PtScoreMapper, PtScore> implements IPtScoreService {

    @Autowired
    private PtExpClassMapper ptExpClassMapper;

    @Autowired
    private PtVideoMapper ptVideoMapper;

    @Autowired
    private ISysOssService iSysOssService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int scanCode(PtScoreVO ptScoreVO) {
        Long userId = SecurityUtils.getUserId();
        Long deptId = SecurityUtils.getDeptId();

        PtExpClass ptExpClass = ptExpClassMapper.selectOne(Wrappers.lambdaQuery(PtExpClass.class)
            .eq(PtExpClass::getClassId, deptId).eq(PtExpClass::getExpId, ptScoreVO.getExpId())
        );
        if (ptExpClass == null) {
            throw new ServiceException("当前实验未开启");
        }
        if (System.currentTimeMillis() > ptExpClass.getEndTime().getTime()) {
            throw new ServiceException("当前实验以结束，请联系老师核查");
        }
        PtScore ptScore = baseMapper.selectOne(Wrappers.lambdaQuery(PtScore.class)
            .eq(PtScore::getUserId, ptScoreVO.getStuId()).eq(PtScore::getExpId, ptScoreVO.getExpId())
        );
        if (ptScore == null || ptScore.getScorePreview() == null) {
            throw new ServiceException("预习未完成，打分失败");
        }
        ptScore.setScoreDiscipline(ptScoreVO.getDiscipline());
        ptScore.setScoreOperate(ptScoreVO.getOperate());

        return baseMapper.updateById(ptScore);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createScore(Long expId, Double score) {
        Long userId = SecurityUtils.getUserId();

        PtScore ptScore = baseMapper.selectOne(Wrappers.lambdaQuery(PtScore.class)
            .eq(PtScore::getUserId, userId).eq(PtScore::getExpId, expId)
        );

        if (ptScore == null) {
            throw new ServiceException("视频未看无法答题");
        }

        List<Integer> parse = (List<Integer>) JSONObject.parse(ptScore.getVideoList());

        if (parse.size() < ptVideoMapper.selectCount(Wrappers.lambdaQuery(PtVideo.class).eq(PtVideo::getExpId, expId).select(PtVideo::getId))) {
            throw new ServiceException("视频未看完无法答题");
        }

        ptScore.setScorePreview(score);

        return baseMapper.updateById(ptScore);
    }


}
